package ar.edu.unsam.algo3.plantasvszombies.wicket

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila
import ar.edu.unsam.algo3.plantasvszombies.dominio.JardinZen
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.apache.wicket.model.PropertyModel
import org.uqbar.commons.model.UserException
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XAjaxLink
import org.uqbar.wicket.xtend.XLink
import org.uqbar.wicket.xtend.XListView

class JardinPage extends MainPage {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	@Property JardinAppModel jardinApp = new JardinAppModel
	@Property FilaPanel pnlFila

	new(JardinAppModel unJardinApp) {
		jardinApp = unJardinApp
		defaultModel = jardinApp.asCompoundModel
		val Form<JardinAppModel> jardinForm = new Form<JardinAppModel>("tableroForm")
		jardinForm.defaultModel = jardinApp.asCompoundModel

		addChild(menuNavegacion)
		agregarDatosJuego(jardinForm)
		agregarTablaFilas(jardinForm)
		listaJardinesZen(jardinForm)

		addChild(jardinForm)
	}

	def agregarDatosJuego(Form<JardinAppModel> form) {
		form.addChild(new Label("jugador", jardinApp.jardin.jugador))
		form.addChild(new Label("recursos", new PropertyModel(this, "jardinApp.jardin.recursos")))
		form.addChild(new Label("lugaresDisponibles", new PropertyModel(this, "jardinApp.jardin.filasDisponibles")))
		val feedbackPanel = new FeedbackPanel("feedbackPanel")
		form.addChild(feedbackPanel)
		pnlFila = new FilaPanel("pnlFila", new Fila)
		form.addChild(pnlFila)
	}

	def listaJardinesZen(Form<JardinAppModel> form) {
		val listView = new XListView("jardinesZen", jardinApp.jardin.tiposPosibles)
		listView.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			val miBoton = new XLink("btnNvaFila") => [
				onClick = [ |
					try {
						agregarFila(item.modelObject)
					} catch (UserException e) {
						error(e.getMessage())
					}
				]
			]
			miBoton.addChild(new Label("tipo"))
			item.addChild(
				miBoton
			)
		]
		form.addChild(listView)
	}

	def agregarFila(JardinZen unZen) {
		val fila = new Fila
		fila.jardinZen = unZen
		jardinApp.jardin.agregarFila(fila)
	}

	def agregarTablaFilas(Form<JardinAppModel> form) {
		val listView = new XListView("misFilas")
		listView.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("tipo"))
			item.addChild(new Label("plantas"))
			item.addChild(
				new XLink("eliminar") => [
					onClick = [ |
						jardinApp.jardin.eliminarFila(item.modelObject)
					]]
			)
			item.addChild(
				new XAjaxLink("editar") => [
					onClick = [ target |
						form.replace(new FilaPanel("pnlFila", item.modelObject))
						target.add(form)
						target.appendJavaScript("$('#myModal').modal('show');")
					]
				])
			item.addChild(
				new XAjaxLink("atacar") => [
					onClick = [ target |
						if (!item.modelObject.plantas.empty) {
							form.replace(new BatallaPanel("pnlFila", item.modelObject))
							target.add(form)
							target.appendJavaScript(alCerrarModal)
						} else {
							error("No se puede atacar una fila vacia")
							target.add(form)
						}
					]
				])
		]
		form.addChild(listView);
	}

	def String alCerrarModal() {
		"$('#myModal').modal('show');"
	}
}
